#include "CGraphics.h"

CGraphics::CGraphics()
{
	
}

CGraphics::~CGraphics()
{

}
void CGraphics::update()
{
	processor.update();
	ram.update();
	testText.setText(raspiCpu.getCpuGhz());
	ramText.setText(raspiRam.getDataInMb());
	testText.update();
	ramText.update();
}
int CGraphics::init()
{
	mainWindow.create(sf::VideoMode(320,240), "RPICC");
	LOG(INFO) << "Initialising main window and loading textures";
	ram.setTexture("memory.png");
	ram.setPosition(processor.getPosition().x, processor.getPosition().y+60);
	ram.setScale(0.5,0.5);
	temperature.setTexture("atlas.png", 0,0,25,64);
	temperature.setPosition(ram.getPosition().x, processor.getPosition().y+120);
	processor.setTexture("processor.png");
	processor.setScale(0.5, 0.5);
	processor.setPosition(0,0);
	windowSize = mainWindow.getSize();
	testText.loadFont("fonts/FreeMono.ttf");
	ramText.loadFont("fonts/FreeMono.ttf");
	mainWindow.setFramerateLimit(1);
	testText.setPosition(60,processor.getPosition().y);
	ramText.setPosition(60, ram.getPosition().y);

	return 0;
}
int CGraphics::mainLoop()
{
	int initCode = this->init();
	int fd = arduinoSerial.openPort();
	arduinoSerial.setInterfaceAttribs(fd, 9600, 0);
	arduinoSerial.setBlocking(fd, 0);
	int connectionProbes = 0; //tries of connection
	while(true)
	{
		arduinoSerial.writeData(fd, "start");
		std::string data = arduinoSerial.readData(fd);
		std::cout << data << std::endl;
		if(data.find("Ok") != std::string::npos)
		{
			std::cout << "Connected!" << std::endl;
			LOG(INFO) << "Connected to Arduino!" << std::endl;
			break;
		}
		std::cout << "Waiting for arduino..." << std::endl;
		connectionProbes++;
		if(connectionProbes > 10)
		{
			close(fd);
			std::cout << "Cannot connect to arduino!" << std::endl;
			LOG(ERROR) << "Cannot connect to arduino";
			return -1;
		}
	}
	if(initCode == 0)
	{
		LOG(INFO) << "Init successfully";
		while(mainWindow.isOpen())
		{			
			sf::Event event;

			while(mainWindow.pollEvent(event))
			{
				if(event.type == sf::Event::Closed)
				{
					close(fd);
					mainWindow.close();			
				}
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
				{
					///
					/// TODO: GAMEPAD STEERING
					///
					LOG(INFO) << "Moving vechicle forward" << std::endl;
					std::cout << "Moving forward" << std::endl;
					LOG(INFO) << "Sending Command: " << "onwds";
					std::cout << "Sending Command: " << "onwds" << std::endl;
					LOG(INFO) << "Send try ";
					std::cout << "Send try " << std::endl;
					arduinoSerial.writeData(fd, "ownds");
					std::string response = arduinoSerial.readData(fd);
					if(response.find("Ok") != std::string::npos)
					{
						LOG(INFO) << "Sent!  ";
						std::cout << "Sent! " << std::endl;
					}else
					{
						LOG(WARNING) << "Not sent, check connection!  ";
						std::cout << "Not sent, check connection! "  << std::endl;
					}
				}
			}
			update();
			mainWindow.clear(sf::Color::Black);
			processor.draw(mainWindow);
			testText.draw(mainWindow);
			ram.draw(mainWindow);
			ramText.draw(mainWindow);
			temperature.draw(mainWindow);
			mainWindow.display();
			
		}
	}

	return initCode;
}
